import http from '@/common/utils/http';
import { user, system } from '@/store';
import * as u from '@/tool/function/util';
import { decryptSM4,encryptSM4 } from '@/common/utils/util';
import systemConfig from '@/common/config';
const COMMON_URL = systemConfig.commonUrl;
export default {
  send: {
    url: systemConfig.baseUrl + COMMON_URL,
    name: '根据funcCode调用方法',
    postCrypto: async function (funcCode: string, data: object, timeout: number, loading: boolean,repeatEnabled:boolean, desensitizeFlag: boolean) {
      //接口加密
      return await this.post(funcCode,data,timeout,loading,true,repeatEnabled,desensitizeFlag);
    },
    postRepeat: async function (funcCode: string, data: object, timeout: number,cryptoEnabled:boolean,desensitizeFlag:boolean) {
			//防重复提交接口
			return await this.post(funcCode,data,timeout,cryptoEnabled,cryptoEnabled,true,desensitizeFlag);
		},
    post: async function (funcCode: string, data: object, timeout: number, loading: boolean,cryptoEnabled:boolean,repeatEnabled:boolean, desensitizeFlag: boolean) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': funcCode, 'x-desensitize-flag': desensitizeFlag + '',cryptoEnabled:cryptoEnabled+'',repeatEnabled:repeatEnabled+'' };
      config.header = headers;
      if(loading!==null&&loading!==undefined){
        config.loading = loading;
      }
      return await http.post(this.url + funcCode, data, config);
    },
    getCrypto: async function (funcCode: string, data: object, timeout: number, loading: boolean,repeatEnabled:boolean, desensitizeFlag: boolean) {
      //接口加密
      return await this.get(funcCode,data,timeout,loading,true,repeatEnabled,desensitizeFlag);
    },
    getRepeat: async function (funcCode: string, data: object, timeout: number,cryptoEnabled:boolean,desensitizeFlag:boolean) {
			//防重复提交接口
			return await this.get(funcCode,data,timeout,cryptoEnabled,cryptoEnabled,true,desensitizeFlag);
		},
    get: async function (funcCode: string, data: object, timeout: number, loading: boolean,cryptoEnabled:boolean,repeatEnabled:boolean, desensitizeFlag: boolean) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': funcCode, 'x-desensitize-flag': desensitizeFlag + '',cryptoEnabled:cryptoEnabled+'',repeatEnabled:repeatEnabled+'' };
      config.header = headers;
      if(loading!==null&&loading!==undefined){
        config.loading = loading;
      }
      return await http.get(this.url + funcCode, data, config);
    },
  },
  getBatchDict: {
    funcCode: 'dict0013',
    url: systemConfig.baseUrl + COMMON_URL + 'dict0013',
    name: '批量获取数据字典信息',
    post: async function (data: object, timeout: number) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': 'dict0013' };
      config.header = headers;
      return await http.post(this.url, data, config);
    },
  },
  getDict: {
    funcCode: 'dict0012',
    url: systemConfig.baseUrl + COMMON_URL + 'dict0012',
    name: '单个获取数据字典信息',
    post: async function (data: object, timeout: number) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': 'dict0012' };
      config.header = headers;
      return await http.post(this.url, data, config);
    },
  },
  getDictCondition: {
    funcCode: 'dict0021',
    url: systemConfig.baseUrl + COMMON_URL + 'dict0021',
    name: '单个获取过滤条件字典下拉信息',
    post: async function (data: object, timeout: number) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': 'dict0021' };
      config.header = headers;
      return await http.post(this.url, data, config);
    },
  },
  getDictSearchData: {
    funcCode: 'dict0022',
    url: systemConfig.baseUrl + COMMON_URL + 'dict0022',
    name: '获取通用搜索数据',
    post: async function (data: object, timeout: number) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': 'dict0022' };
      config.header = headers;
      return await http.post(this.url, data, config);
    },
  },
  fileUploadUrl: {
    url: systemConfig.fileUrl + '/api/common/file/upload/' + systemConfig.fileAppId,
    name: '上传文件url',
  },
  fileShowUrl: {
    url: systemConfig.fileUrl + '/api/common/file/show/' + systemConfig.fileAppId + '/',
    name: '查看文件url',
  },
  fileDownloadUrl: {
    url: systemConfig.fileUrl + '/api/common/file/download/' + systemConfig.fileAppId + '/',
    name: '下载文件url',
  },
  fileDeleteUrl: {
    url: systemConfig.fileUrl + '/api/common/file/delete/' + systemConfig.fileAppId + '/',
    name: '删除文件',
  },
  login: {
    funcCode: 'login0001',
    url: systemConfig.baseUrl + COMMON_URL + 'login0001',
    name: '登录',
    post: async function (data: object) {
      let config: any = {};
      let headers = { 'func-code': 'login0001',cryptoEnabled:'true',repeatEnabled:'true' };
      config.header = headers;
      let params=Object.assign({loginAppId:systemConfig.appId}, data);
      let response: any = await http.post(this.url, params, config);
      if (response.code === 0) {
        const result = response.result;
        user.setToken(result.accessToken,result.refreshToken);
        await user.setUser(null);
        if(result.secretKey){
					u.setCookie('USERSECRET',encryptSM4(JSON.stringify(result.secretKey),system.defaultSM4Key));
				}
				await user.setUserSecret(result.secretKey);
        user.appId = result.appId;
        user.orgId = result.orgId;
        return true;
      } else {
        return false;
      }
    },
  },
  logout: {
    funcCode: 'login0003',
    url: systemConfig.baseUrl + COMMON_URL + 'login0003',
    name: '用户退出',
    post: async function () {
      let config: any = {};
      let headers = { 'func-code': 'login0003',cryptoEnabled:'true',repeatEnabled:'true' };
      config.header = headers;
      await http.post(this.url, {}, config);
      await user.setUser(null);
      await user.setToken(null,null);
      uni.reLaunch({ url: systemConfig.loginPath });
    },
  },
  getUserInfo: {
    funcCode: 'base0001',
    url: systemConfig.baseUrl + COMMON_URL + 'base0001',
    name: '获取登录用户信息',
    post: async function () {
      let config: any = {};
      let headers = { 'func-code': 'base0001',cryptoEnabled:'true' };
      config.header = headers;
      let response: any = await http.post(this.url, {}, config);
      if (response.code === 0) {
        const result = response.result;
        if (result) {
          await user.setUser(result);
          if(result.secretKey){
            u.setCookie('USERSECRET',encryptSM4(JSON.stringify(result.secretKey),system.defaultSM4Key));
            await user.setUserSecret(result.secretKey);
          }
          u.setCookie('USER_INFO', encryptSM4(JSON.stringify(result),system.defaultSM4Key));
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
  },
  getUserInfoEn: {
    funcCode: 'base0016',
    url: systemConfig.baseUrl + COMMON_URL + 'base0016',
    name: '获取登录用户信息',
    post: async function () {
      let params: any = {};
			params.token = encryptSM4(user.accesstoken||'',system.defaultSM4Key);
			let config: any = {};
			let headers = { 'func-code': 'base0016',cryptoEnabled:'true' };
			config.header = headers;
      let response: any = await http.post(this.url, params, config);
      if (response.code === 0) {
        const result = response.result;
        if (result) {
          await user.setUser(JSON.parse(decryptSM4(result,system.defaultSM4Key)));
					u.setCookie('USER_INFO', result);
					if(result.secretKey){
						u.setCookie('USERSECRET',encryptSM4(JSON.stringify(result.secretKey),system.defaultSM4Key));
						await user.setUserSecret(result.secretKey);
					}
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
  },
  getMenu: {
    funcCode: 'router0007',
    url: systemConfig.baseUrl + COMMON_URL + 'router0007',
    name: '获取菜单',
    post: async function (data: object, timeout: number) {
      let config: any = {};
      if (timeout && timeout > 0) {
        config.timeout = timeout;
      }
      let headers = { 'func-code': 'router0007',cryptoEnabled:'true' };
      config.header = headers;
      return await http.post(this.url + 'router0007', data, config);
    },
  },
  setAppSecretKey: {
		funcCode: 'base0017',
		url: systemConfig.baseUrl + COMMON_URL + 'base0017',
		name: '设置应用的秘钥',
		post: async function () {
			let config: any = {};
			let headers = { 'func-code': 'base0017' };
			config.header = headers;
			await http.post(this.url, {appId: encryptSM4(systemConfig.appId+'',system.defaultSM4Key)}, config);
			system.setAppCryptoFlag(true);
		},
	},
	refreshAccessToken: {
		funcCode: 'base0018',
		url: systemConfig.baseUrl + COMMON_URL + 'base0018',
		name: '刷新accessToken',
		post: async function () {
			let config: any = {};
			let headers = { 'func-code': 'base0018',cryptoEnabled:'true',repeatEnabled:'true' };
			config.header = headers;
			let params={} as any;
      let refreshAccessToken=user.refreshToken;
      if(!refreshAccessToken){
        refreshAccessToken=u.getCookie(systemConfig.localRefreshTokenName);
      }
			params[systemConfig.refreshTokenName]=refreshAccessToken
			let res:any =await http.post(this.url, params, config);
			if (res.code === 0) {
				const result = res.result;
        user.setToken(result.accessToken,result.refreshToken);
        if(result.secretKey){
					u.setCookie('USERSECRET',encryptSM4(JSON.stringify(result.secretKey),system.defaultSM4Key));
				}
				await user.setUserSecret(result.secretKey);
				return true;
			} else {
				return false;
			}
		},
    checkToken: {
      funcCode: 'base0020',
      url: systemConfig.baseUrl + COMMON_URL + 'base0020',
      name: '校验token',
      post: async function () {
        let config: any = {};
        let headers = { 'func-code': 'base0020',cryptoEnabled:'true',repeatEnabled:'false' };
        config.header = headers;
        let params={} as any;
        let refreshAccessToken=user.refreshToken;
        if(!refreshAccessToken){
          refreshAccessToken=u.getCookie(systemConfig.localRefreshTokenName);
        }
        let accessToken=user.accesstoken;
        if(!accessToken){
          accessToken=u.getCookie(systemConfig.localTokenName);
        }
        params[systemConfig.accessTokenName]=accessToken
        params[systemConfig.refreshTokenName]=refreshAccessToken
        let res:any =await http.post(this.url, params, config);
        if (res.code === 0) {
          const result = res.result;
          if(result==null){
            return false;
          }else{
            user.setToken(result.accessToken,result.refreshToken);
            if(result.secretKey){
              u.setCookie('USERSECRET',encryptSM4(JSON.stringify(result.secretKey),system.defaultSM4Key));
            }
            await user.setUserSecret(result.secretKey);
            return true;
          }
          
        } else {
          return false;
        }
      } 
    }
	},
};
